﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Text;

/// <summary>
///PostWork 的摘要说明
/// </summary>
public class PostWork:InterfacePost
{
    public void Work()
    {
        //toole.WriteFile(toole.ID + "\r\n");
        try
        {
            HttpRequest one;
            if (toole.listrequest.Count > 0)
            {
                lock (toole.listrequest)
                {
                    one = toole.listrequest[0];
                    toole.listrequest.RemoveAt(0);
                }
                SetPost(one);
            }
        }
        catch (Exception ee)
        {
            toole.WriteFile(ee.Message + "\r\n" + ee.Source + "\r\n" + ee.StackTrace);
        }
    }
    static int x = 0;
    public void SetPost(HttpRequest hr)
    {
        //toole.WriteFile(x++ + "\r\n");
        System.IO.Stream s=hr.InputStream;
        try
        {
            int count = 0;
            byte[] buffer = new byte[1024];
            StringBuilder builder = new StringBuilder();
            while ((count = s.Read(buffer, 0, 1024)) > 0)
            {
                builder.Append(Encoding.UTF8.GetString(buffer, 0, count));
            }
            s.Flush();
            s.Close();
            s.Dispose();
            string POSTStr = builder.ToString();
            if (!string.IsNullOrEmpty(POSTStr))
            {
                string[] Arr = POSTStr.Split('&');
                model m = new model();
                for (int i = 0; i < Arr.Length; i++)
                {
                    string[] strarr = Arr[i].Split(new string[] { "=" }, StringSplitOptions.RemoveEmptyEntries);
                    switch (strarr[0])
                    {
                        case "fild1":
                            m.IP = strarr[1];
                            break;
                        case "fild2":
                            m.ID = strarr[1];
                            break;
                    }
                }
                m.TimeStr = DateTime.Now.ToString();
                lock (toole.listmodel)
                {
                    toole.listmodel.Add(m);
                }
                ThreadingListWork.AddWork(new AddSqlite());
            }
        }
        catch (Exception ee)
        {
            toole.WriteFile(ee.Message + "\r\n" + ee.Source + "\r\n" + ee.StackTrace);
        }
    }
}